<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="utf-8">
    <title>layui</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <link rel="stylesheet" href="/lib/layui-v2.5.5/css/layui.css" media="all">
    <link rel="stylesheet" href="/css/public.css" media="all">
    <style>
        body {
            background-color: #ffffff;
        }
    </style>
</head>
<body>
<form class="layui-form">
    <div class="layui-form-item">
        <label class="layui-form-label required">用户名</label>
        <div class="layui-input-block">
            <input type="text" lay-verify="required" lay-reqtext="角色名不能为空" name="name" placeholder="请输入角色名" value="管理员"
                   class="layui-input">
            <tip>填写自己想要新增的角色名。</tip>
        </div>
    </div>


    <div class="layui-form-item">
        <label class="layui-form-label required">账号</label>
        <div class="layui-input-block">
            <input type="text" lay-verify="required" placeholder="请输入账号" class="layui-input" name="account"
                   value="root">
            <tip>填写新增的账号。</tip>
        </div>
    </div>

    <div class="layui-form-item">
        <label class="layui-form-label required">状态</label>
        <div class="layui-input-inline" style="width: 200px;">
            <select name="status" class="city-selector" data-value="" lay-filter="city-1">
                <option value="">请选择状态</option>
                <option value="0">启用</option>
                <option value="1">禁用</option>
            </select>
        </div>
    </div>

    <div class="layui-form-item">
        <label class="layui-form-label ">添加权限</label>
        <div class="layui-input-inline" style="width: 200px;">
            <div id="tree" class="demo-tree" ></div>
        </div>
    </div>


    <div class="layui-form-item">
        <div class="layui-input-block">
            <button class="layui-btn layui-btn-normal" lay-submit lay-filter="saveBtn">确认保存</button>
        </div>
    </div>


</form>
<script src="/lib/layui-v2.5.5/layui.js" charset="utf-8"></script>
<script>
    layui.use(['form', 'tree', 'util'], function () {
        var form = layui.form,
            layer = layui.layer,
            tree = layui.tree,
            util = layui.util,
            $ = layui.$;

      //初始化树形结构
        $.ajax({
            url: '/menuList',
            success: function (data) {
                tree.render({
                    elem: '#tree'
                    , data: data
                    , showCheckbox: true
                    ,id:'id'
                });
            }
        })


        //监听提交
        form.on('submit(saveBtn)', function (data) {
            layer.msg("数据提交中。。。。");
            var checkData = tree.getChecked('id');
            var list = new Array();
            list = getChecked_list(checkData);
            $.ajax({
                url: "/addRole?list=" + list,
                type: "post",
                dataType: "json",
                contentType: "application/json;",
                data: JSON.stringify(data.field),
                success: function (data) {
                    if (data == 1) {
                        layer.msg("添加角色成功")
                        var iframeIndex = parent.layer.getFrameIndex(window.name);
                        parent.layer.close(iframeIndex);
                    } else {
                        layer.msg("添加角色失败")
                    }
                }
            })
        });

        //获取选中节点的ID
        function getChecked_list(data) {
            var id = '';
            $.each(data, function (index, item) {
                if (id != '') {
                    id = id + "," + item.id;
                } else {
                    id = item.id;
                }
                var i = getChecked_list(item.children);
                if (i != '') {
                    id = id + "," + i;
                }
            });
            return id;
        }

        // //按钮事件
        // util.event('lay-demo', {
        //     getChecked: function(othis){
        //         var checkedData = tree.getChecked('tree'); //获取选中节点的数据
        //
        //         layer.alert(JSON.stringify(checkedData), {shade:0});
        //         console.log(checkedData);
        //     }
        //     ,setChecked: function(){
        //         tree.setChecked('demoId1', [12, 16]); //勾选指定节点
        //     }
        //     ,reload: function(){
        //         //重载实例
        //         tree.reload('demoId1', {
        //
        //         });
        //
        //     }
        // });

        return false;

    });


</script>
</body>
</html>